$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/productcategory/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '分类名称', name: 'name', index: 'name', width: 80 },
            { label: '商品数量', name: 'productcount', index: 'productCount', width: 80 },
            { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.update("+ rowdata.id +")'>" +
                        "<img src='../../statics/img/ico14-edit-black.png'></a>&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.del("+ rowdata.id +")'>" +
                        "<img src='../../statics/img/ico16-delete.png'></a>";
            }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		productcategory: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = true;
			vm.title = "新增";
			vm.productcategory = {};
            $("#myProductModalLabel").text("新增商品分类");
            $('#myProductModal').modal();
		},
		update: function (id) {
			vm.showList = true;
            vm.title = "修改";
            
            vm.getInfo(id);
            $("#myProductModalLabel").text("修改商品分类");
            $('#myProductModal').modal();
		},
		saveOrUpdate: function (event) {
			var url = vm.productcategory.id == null ? "sys/productcategory/save" : "sys/productcategory/update";
			if(vm.productcategory.name ==''|| typeof vm.productcategory.name=='undefined'){
                alert("商品分类名称不能为空！");
			}else{
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.productcategory),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
			}

		},
		del: function (id) {
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/productcategory/delete/"+id,
                    contentType: "application/json",
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/productcategory/info/"+id, function(r){
                vm.productcategory = r.productcategory;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});